package itsm.isperp.module.controller.configuration;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.configuration.ConfCiUseRelation;
import itsm.isperp.module.service.configuration.ConfCiService;
import itsm.isperp.module.service.configuration.ConfCiUseRelationService;
import itsm.isperp.module.service.project.ProjProjectService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 资产使用记录表控制器
 * 
 * @author lizx
 * @date 2014-10-18 18:10
 */
@Controller
@RequestMapping("configuration/ciUseRelation")
public class ConfCiUseRelationController extends
		BaseController<ConfCiUseRelation> {

	@Autowired
	protected ConfCiUseRelationService confCiUseRelationService;
	@Autowired
	protected ConfCiService confCiService;

	@Override
	public ConfCiUseRelationService getService() {
		return this.confCiUseRelationService;
	}

	@Override
	public String getPageTitle() {
		return "资产使用记录表";
	}

	@RequestMapping("saveWithCi")
	@ResponseBody
	public ResultMessage saveWithCi(String ciId, String relationType,
			String relationId, HttpServletRequest req) {

		String[] cIds = ciId.split(",");

		for (String cId : cIds) {
			ConfCiUseRelation model = new ConfCiUseRelation();
			model.setCiId(cId);
			model.setRelationId(relationId);
			model.setRelationType(relationType);
			this.getService().saveOrUpdate(model);
		}
		return ResultMessage.success(ciId);
	}

	@RequestMapping("saveWithCiMul")
	@ResponseBody
	public ResultMessage saveWithCiMul(String ciId, String relationType,
			String relationId, String ciCategoryId, HttpServletRequest req) {

		String[] cIds = ciId.split(",");

		for (String cId : cIds) {
			ConfCiUseRelation model = new ConfCiUseRelation();
			model.setCiId(cId);
			model.setRelationId(relationId);
			model.setRelationType(relationType);
			this.getService().saveOrUpdate(model);
		}
		String chgRelateCiMulId = HibernateUUIDGenerator.generate();
		this.getService().insertIntoChgRelateCiMul(chgRelateCiMulId,
				relationId, ciCategoryId, ciId);

		return ResultMessage.success(chgRelateCiMulId);
	}

	@RequestMapping("deleteCiRelationAndChangeRelateProperty")
	@ResponseBody
	public ResultMessage deleteCiRelationAndChangeRelateProperty(
			HttpServletRequest req) {
		String ciId = req.getParameter("ciId");
		String changeId = req.getParameter("changeId");
		String relationType = req.getParameter("relationType");
		if (StringUtils.isEmpty(relationType))
			relationType = "changeProcess";
		this.getService().deleteCiRelationAndChangeRelateProperty(changeId,
				ciId, relationType);
		return ResultMessage.success();
	}

	@RequestMapping("listCi")
	@ResponseBody
	public PageResponse<Map<String, Object>> listCi(JqGridRequest request,
			HttpServletRequest req) {
		String relationId = req.getParameter("relationId");

		String projectId = req.getParameter("projectId");

		if (StringUtils.isNotEmpty(projectId)) {
			ProjProjectService projProjectService = ContextHolder
					.getSpringBean("projProjectService");
			Map<String, Object> map = projProjectService.findOneToMap(
					projectId, "cus_customer_id");

			request.addFilterRule("cus_customer_id", JqGridFilterRuleOp.eq, map
					.get("cus_customer_id").toString());
		}
		request.setSearchString(" a.id NOT IN (SELECT ci_id FROM conf_ci_use_relation WHERE relation_id='"
				+ relationId + "')");

		return this.confCiService.findAllDetailByRequest(request).toPage();

	}

	@RequestMapping("listTecServiceCi")
	@ResponseBody
	public PageResponse<Map<String, Object>> listTecServiceCi(
			JqGridRequest request, HttpServletRequest req) {
		String tecServiceId = req.getParameter("tecServiceId");
		String relationId = req.getParameter("relationId");
		String[] temArr = tecServiceId.split(",");
		String inStr = "";
		for (String s : temArr) {
			inStr += "'" + s + "',";
		}
		inStr = inStr.substring(0, inStr.length() - 1);
		DataResponse<Map<String, Object>> ds = confCiService.findTecServiceCi(
				relationId, inStr, request);
		if (ds != null)
			return ds.toPage();
		else
			return null;
	}
	
	 @RequestMapping("listChangeCi")
	  @ResponseBody
	  public PageResponse<Map<String, Object>> listChangeCi(JqGridRequest request,
	      HttpServletRequest req) {
	    String relationId = req.getParameter("relationId");

	    String projectId = req.getParameter("projectId");

	    if (StringUtils.isNotEmpty(projectId)) {
	      ProjProjectService projProjectService = ContextHolder
	          .getSpringBean("projProjectService");
	      Map<String, Object> map = projProjectService.findOneToMap(
	          projectId, "cus_customer_id");

	      request.addFilterRule("cus_customer_id", JqGridFilterRuleOp.eq, map
	          .get("cus_customer_id").toString());
	    }
	    request.setSearchString(" a.id not in(select ci_id from conf_ci_use_relation where  relation_type='changeProcess' and relation_id='"+relationId+"' "
	              +" union all"
	              +" select ci_id from conf_ci_use_relation where relation_id  in (select id from chg_change k where k.status<>'已关闭'))");

	    return this.confCiService.findAllDetailByRequest(request).toPage();

	  }

	@RequestMapping("listCiChangeProperty")
	@ResponseBody
	public List<Map<String, Object>> listCiChangeProperty(HttpServletRequest req) {
		String changeId = req.getParameter("changeId");
		return confCiService.findCiPropertyPlanChange(changeId);
	}

	@RequestMapping("listDetail")
	@ResponseBody
	@Override
	public PageResponse<Map<String, Object>> listDetail(JqGridRequest request,
			HttpServletRequest req) {

		String relationId = req.getParameter("relationId");
		String relationType = req.getParameter("relationType");
		if (StringUtils.isNotEmpty(relationId)) {
			request.addFilterRule("relationId", JqGridFilterRuleOp.eq,
					relationId);
		}
		if (StringUtils.isNotEmpty(relationType)) {
			request.addFilterRule("relationType", JqGridFilterRuleOp.eq,
					relationType);
		}

		return this.getService().findAllDetailByRequest(request).toPage();

	}

	@RequestMapping("getChgRelateCiMul")
	@ResponseBody
	public Map<String, Object> getChgRelateCiMul(HttpServletRequest req) {
		String id = req.getParameter("relationId");
		Map<String, Object> mp = this.getService().getChgRelateCiMul(id);
		return mp;
	}

	@RequestMapping("getChgRelateCiMulByChangeId")
	@ResponseBody
	public List<Map<String, Object>> getChgRelateCiMulByChangeId(
			HttpServletRequest req) {
		String id = req.getParameter("changeId");
		List<Map<String, Object>> mp = this.getService()
				.getChgRelateCiMulByChangeId(id);
		return mp;
	}

	@RequestMapping("deleteChgConfuguration")
	@ResponseBody
	public ResultMessage deleteChgConfuguration(HttpServletRequest req) {
		String changeId = req.getParameter("changeId");
		this.getService().deleteChgConfuguration(changeId);
		return ResultMessage.success();
	}

	@RequestMapping("deleteChgConfugurationMulByRelationId")
	@ResponseBody
	public ResultMessage deleteChgConfugurationMulByRelationId(
			HttpServletRequest req) {
		String relationId = req.getParameter("relationId");
		String relationType = req.getParameter("relationType");
		this.getService().deleteChgConfugurationMulByRelationId(relationId,
				relationType);
		return ResultMessage.success();
	}

}
